﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FineUI;
using ZHDJ.JoinParty.BLL;
using System.Data;
using ZHDJ.Data.Models.JoinParty;
using ZHDJ.Core.Util;

namespace JoinParty.admin.FlowStep
{
    public partial class JoinPartyInspectionOEdit : PageBase
    {
        public string StandID
        {
            get
            {
                return GetQueryIntValue("StandID");
            }
        }

        public string OrderNo
        {
            get { return GetQueryIntValue("OrderNo"); }
        }

        #region ViewPower

        /// <summary>
        /// 本页面的浏览权限，空字符串表示本页面不受权限控制
        /// </summary>
        public override string ViewPower
        {
            get
            {
                return "";
            }
        }

        #endregion
        #region Page_Load

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
                LoadData();
            }
        }

        private void BindData()
        {
            btnClose.OnClientClick = ActiveWindow.GetHideReference();
            var example = DB.PartyJoinLinkExamples.Find(this.CurrentStandID);
            if (example != null)
            {
                var app = ProcesUtil.GetApplycation(example.AppllicationID);
                this.hdAppID.Value = app.AppllicationID;
                this.hdAppLoginID.Value = app.LoginID;
                this.hdCurrentStandID.Value = this.CurrentStandID;
                this.hdUserName.Value = app.UserName;
            }
            if (ReadOnly == "1")
            {
                if (!IsAdmin)
                    this.btnSaveClose.Hidden = true;
            }
        }
        private void LoadData(string IsSave = "")
        {
            btnClose.OnClientClick = ActiveWindow.GetHideReference();
            var ps = DB.PartyJoinProcesss.Find(this.CurrentStepID);
            if (!string.IsNullOrEmpty(this.StandID))
            {
                var rep = DB.PartyJoinReviewOptions.Where(a => a.StandID == this.StandID).FirstOrDefault();
                if (rep != null)
                {
                    this.ckOptionGroup.Text = rep.PartyGroupContent;
                    switch (OrderNo)
                    {
                        case "1": this.dtTime.Text = Convert.ToDateTime(rep.OpinionOneCreateTime == null ? DateTime.Now : rep.OpinionOneCreateTime).ToString("yyyy-MM-dd"); this.ckOptionGroup.Text = rep.OpinionOne; break;
                        case "2": this.dtTime.Text = Convert.ToDateTime(rep.OpinionTwoCreateTime == null ? DateTime.Now : rep.OpinionTwoCreateTime).ToString("yyyy-MM-dd"); this.ckOptionGroup.Text = rep.OpinionTwo; break;
                        case "3": this.dtTime.Text = Convert.ToDateTime(rep.OpinionThreeCreateTime == null ? DateTime.Now : rep.OpinionThreeCreateTime).ToString("yyyy-MM-dd"); this.ckOptionGroup.Text = rep.OpinionThree; break;
                        case "4": this.dtTime.Text = Convert.ToDateTime(rep.OpinionFourCreateTime == null ? DateTime.Now : rep.OpinionFourCreateTime).ToString("yyyy-MM-dd"); this.ckOptionGroup.Text = rep.OpinionFour; break;
                        case "5": this.dtTime.Text = Convert.ToDateTime(rep.OpinionFiveCreateTime == null ? DateTime.Now : rep.OpinionFiveCreateTime).ToString("yyyy-MM-dd"); this.ckOptionGroup.Text = rep.OpinionFive; break;
                        case "6": this.dtTime.Text = Convert.ToDateTime(rep.OpinionSixCreateTime == null ? DateTime.Now : rep.OpinionSixCreateTime).ToString("yyyy-MM-dd"); this.ckOptionGroup.Text = rep.OpinionSix; break;
                    }
                }
            }
        }



        private void SaveData(bool isClose, bool Submit = false)
        {
            bool bResult = false;
            var rep = new PartyJoinReviewOption();
            if (!string.IsNullOrEmpty(this.StandID))
            {
                rep = DB.PartyJoinReviewOptions.Where(a => a.StandID == this.StandID).FirstOrDefault();
                if (rep != null)
                {
                    rep.LoginID = this.hdAppLoginID.Value;
                    rep.LoginName = this.hdUserName.Value;
                    rep.StandID = this.CurrentStandID;
                    switch (OrderNo)
                    {
                        case "1": rep.OpinionOne = this.ckOptionGroup.Text; rep.OpinionOneCreateTime = Convert.ToDateTime(dtTime.Text); break;
                        case "2": rep.OpinionTwo = this.ckOptionGroup.Text; rep.OpinionTwoCreateTime = Convert.ToDateTime(dtTime.Text); break;
                        case "3": rep.OpinionThree = this.ckOptionGroup.Text; rep.OpinionThreeCreateTime = Convert.ToDateTime(dtTime.Text); break;
                        case "4": rep.OpinionFour = this.ckOptionGroup.Text; rep.OpinionFourCreateTime = Convert.ToDateTime(dtTime.Text); break;
                        case "5": rep.OpinionFive = this.ckOptionGroup.Text; rep.OpinionFiveCreateTime = Convert.ToDateTime(dtTime.Text); break;
                        case "6": rep.OpinionSix = this.ckOptionGroup.Text; rep.OpinionSixCreateTime = Convert.ToDateTime(dtTime.Text); break;
                    }
                    bResult = DB.SaveChanges() > 0;
                }
                else
                {
                    rep = new PartyJoinReviewOption();
                    rep.ID = StringHelper.NewID;
                    rep.LoginID = this.hdAppLoginID.Value;
                    rep.LoginName = this.hdUserName.Value;
                    rep.StandID = this.CurrentStandID;
                    switch (OrderNo)
                    {
                        case "1": rep.OpinionOne = this.ckOptionGroup.Text; rep.OpinionOneCreateTime = Convert.ToDateTime(dtTime.Text); break;
                        case "2": rep.OpinionTwo = this.ckOptionGroup.Text; rep.OpinionTwoCreateTime = Convert.ToDateTime(dtTime.Text); break;
                        case "3": rep.OpinionThree = this.ckOptionGroup.Text; rep.OpinionThreeCreateTime = Convert.ToDateTime(dtTime.Text); break;
                        case "4": rep.OpinionFour = this.ckOptionGroup.Text; rep.OpinionFourCreateTime = Convert.ToDateTime(dtTime.Text); break;
                        case "5": rep.OpinionFive = this.ckOptionGroup.Text; rep.OpinionFiveCreateTime = Convert.ToDateTime(dtTime.Text); break;
                        case "6": rep.OpinionSix = this.ckOptionGroup.Text; rep.OpinionSixCreateTime = Convert.ToDateTime(dtTime.Text); break;
                    }
                    DB.PartyJoinReviewOptions.Add(rep);
                    bResult = DB.SaveChanges() > 0;
                }
            }
            else
            {
                rep.ID = StringHelper.NewID;
                rep.LoginID = this.hdAppLoginID.Value;
                rep.LoginName = this.hdUserName.Value;
                rep.StandID = this.CurrentStandID;
                switch (OrderNo)
                {
                    case "1": rep.OpinionOne = this.ckOptionGroup.Text; rep.OpinionOneCreateTime = Convert.ToDateTime(dtTime.Text); break;
                    case "2": rep.OpinionTwo = this.ckOptionGroup.Text; rep.OpinionTwoCreateTime = Convert.ToDateTime(dtTime.Text); break;
                    case "3": rep.OpinionThree = this.ckOptionGroup.Text; rep.OpinionThreeCreateTime = Convert.ToDateTime(dtTime.Text); break;
                    case "4": rep.OpinionFour = this.ckOptionGroup.Text; rep.OpinionFourCreateTime = Convert.ToDateTime(dtTime.Text); break;
                    case "5": rep.OpinionFive = this.ckOptionGroup.Text; rep.OpinionFiveCreateTime = Convert.ToDateTime(dtTime.Text); break;
                    case "6": rep.OpinionSix = this.ckOptionGroup.Text; rep.OpinionSixCreateTime = Convert.ToDateTime(dtTime.Text); break;
                }
                DB.PartyJoinReviewOptions.Add(rep);
                bResult = DB.SaveChanges() > 0;
            }

            if (bResult)
            {
                if (Submit)
                    Alert.Show("保存成功", String.Empty, ActiveWindow.GetHideReference());
                else
                    Alert.Show("提交成功", String.Empty, ActiveWindow.GetHideReference());
            }
        }


        #endregion

        #region
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            this.SaveData(true);
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }

        protected void btnSaveClose_Click(object sender, EventArgs e)
        {

            //控件可见，党小组意见不能为空
            if (!this.ckOptionGroup.Hidden && string.IsNullOrEmpty(FilterSpecialCharacters.Filter(this.ckOptionGroup.Text.Trim())))
            {
                Alert.Show("考察意见不能为空！", String.Empty);
                return;
            };
            SaveData(true, true);
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }
        #endregion

    }
}